﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProductionManagement.Infrstructre
{
    /// <summary>
    /// 生产管理仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {
        ISqlSugarClient db;

        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T entity)
        {
            return await  db.Insertable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task<int> DelRange(List<T> entities)
        {
            return await  db.Deleteable(entities).ExecuteCommandAsync();
        }

        public async Task<ISugarQueryable<T>> GetAll()
        {
            return db.Queryable<T>();
        }
        /// <summary>
        /// 不带异步的查询实体
        /// </summary>
        /// <returns></returns>
        public List<T> Query()
        {
            return db.Queryable<T>().ToList();
        }
        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdAsync(T entity)
        {
            return await  db.Updateable(entity).ExecuteCommandAsync();
        }
    }
}
